<template>
  <el-dialog
    :title="
      detail
        ? '查看云认养服务套餐'
        : id
        ? '编辑云认养服务套餐'
        : '新增云认养服务套餐'
    "
    :visible.sync="visible"
    width="850px"
    append-to-body
  >
    <el-form
      ref="form"
      :rules="rules"
      :disabled="detail"
      :model="form"
      label-width="140px"
    >
      <el-form-item
        label="套餐类型："
        prop="type"
      >
        <el-select
          v-model="form.type"
          placeholder="请选择"
        >
          <el-option
            v-for="item in COMBO_LIST"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
        <div style="font-size: 12px">普通套餐为图文认养套餐</div>
      </el-form-item>
      <el-form-item
        label="可订购周期："
        prop="cycleNumber"
      >
        <el-row>
          <el-input
            v-model="form.cycleNumber"
            maxlength="10"
            style="width: 180px; margin-right: 20px"
          ></el-input>
          <el-radio
            v-model="form.cycleUnit"
            label="1"
          >
            月
          </el-radio>
          <el-radio
            v-model="form.cycleUnit"
            label="2"
          >
            年
          </el-radio>
        </el-row>
        <div style="font-size: 12px">可订购云认养服务的周期</div>
      </el-form-item>
      <el-form-item
        label="说明："
        prop="description"
      >
        <el-input
          v-model="form.description"
          maxlength="4"
          style="width: 300px"
          show-word-limit
        ></el-input>
      </el-form-item>
      <el-form-item
        label="销售价格（元）："
        prop="price"
      >
        <el-input
          v-model="form.price"
          maxlength="10"
          style="width: 300px"
        ></el-input>
        <div style="font-size: 12px">订购当前云认养套餐所需支付的金额</div>
      </el-form-item>
      <el-form-item
        label="专属权益说明："
        prop="interestDescription"
      >
        <el-input
          v-model="form.interestDescription"
          maxlength="100"
          type="textarea"
          rows="4"
          show-word-limit
        ></el-input>
        <div style="font-size: 12px">
          订购当前云认养套餐用户可获得的权益说明，会同步展示给用户，请认真填写！
        </div>
      </el-form-item>
      <el-form-item
        label="排序值："
        prop="sort"
      >
        <el-input
          v-model="form.sort"
          style="width: 300px"
        ></el-input>
        <div style="font-size: 12px">APP端根据排序值大小，从左到右依次展示</div>
      </el-form-item>
    </el-form>
    <span
      v-if="!detail"
      slot="footer"
      class="dialog-footer"
    >
      <el-button @click="visible = false">取 消</el-button>
      <el-button
        type="primary"
        :loading="loading"
        @click="submitData"
      >
        确 定
      </el-button>
    </span>
  </el-dialog>
</template>
<script>
import { COMBO_LIST } from '@/const/type';
import { addAdoptionPack, getPackDetail, updatePack } from '@/api/core/adopt';
export default {
  props: {
    value: {
      type: Boolean,
      default: false,
    },
    id: {
      type: String,
    },
    detail: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      COMBO_LIST,
      form: {
        type: '1',
        cycleUnit: '1',
      },
      loading: false,
      rules: {
        type: [
          {
            required: true,
            message: '请选择套餐类型',
            trigger: 'blur',
          },
        ],
        cycleNumber: [
          {
            required: true,
            message: '请输入可订购周期',
            trigger: 'blur',
          },
          {
            pattern: /^[0-9]*[1-9][0-9]*$/,
            message: '周期只能输入正整数',
          },
        ],
        description: [
          {
            required: true,
            message: '请输入说明',
            trigger: 'blur',
          },
        ],
        price: [
          {
            required: true,
            message: '请输入销售价格',
            trigger: 'blur',
          },
          {
            pattern: /^[+]?(?:0|(?:[1-9]\d{0,11}))(\.\d{1,2})?$/,
            message: '请输入正确的销售价格',
          },
        ],
        interestDescription: [
          {
            required: true,
            message: '请输入专属权益说明',
            trigger: 'blur',
          },
        ],
        sort: [
          {
            required: true,
            message: '请输入排序值',
            trigger: 'blur',
          },
          {
            pattern: /^[0-9]*[1-9][0-9]*$/,
            message: '排序值只能输入正整数',
          },
        ],
      },
    };
  },
  computed: {
    visible: {
      get() {
        return this.value;
      },
      set(val) {
        this.$emit('input', val);
      },
    },
  },
  watch: {
    visible(val) {
      if (val && this.id) {
        this.getPackDetail();
      } else {
        this.$refs.form && this.$refs.form.resetFields();
      }
    },
  },
  methods: {
    getPackDetail() {
      getPackDetail(this.id).then((res) => {
        this.form = res.data.data;
      });
    },
    // 提交数据
    submitData() {
      this.$refs.form.validate(async (valid, done) => {
        if (valid) {
          try {
            this.loading = true;
            if (this.id) {
              await updatePack(this.form);
            } else {
              await addAdoptionPack(this.form);
            }
            this.$message.success('操作成功');
            this.$emit('onSuccess');
            this.loading = false;
            this.visible = false;
            done();
          } catch (error) {
            this.loading = false;
            done();
            throw new Error(error);
          }
        }
      });
    },
  },
};
</script>
